package com.favccxx.search.api;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.favccxx.search.application.ConceptModelService;
import com.favccxx.search.domain.conceptmodel.ConceptModel;
import com.favccxx.search.infrastructure.exception.ExceptionCode;
import com.favccxx.search.infrastructure.util.rest.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author favccxx
 */
@Slf4j
@Api(tags = "4.0 Concept model interface")
@RestController
@RequestMapping("/conceptmodel")
public class ConceptModelController {

    @Autowired
    ConceptModelService conceptModelService;

    @ApiOperation("Query concept model")
    @GetMapping("/list")
    public RestResult<List<ConceptModel>> findAll() {
        List<ConceptModel> list = conceptModelService.findAll();
        return RestResult.sucess(list);
    }

    @ApiOperation("Insert concept model")
    @PutMapping("/insert")
    public RestResult<ConceptModel> insert(
            @RequestParam(value = "projectId", required = true) String projectId,
            @RequestParam(value = "code", required = true) String code,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "type", defaultValue = "", required = false) String type,
            @RequestParam(value = "description", defaultValue = "", required = false) String description,
            @RequestParam(value = "modelFields", required = false) String modelFields) throws JsonProcessingException {
        ConceptModel conceptModel = new ConceptModel();
        conceptModel.setCode(code);
        conceptModel.setName(name);
        conceptModel.setType(type);
        conceptModel.setProjectId(projectId);
        conceptModel.setDescription(description);

        try {
            ObjectMapper mapper = new ObjectMapper();
            Map<String, String> dataMap = mapper.readValue(modelFields, Map.class);
            conceptModel.setModelFields(dataMap);
        } catch (JsonProcessingException e) {
            log.error("Insert concept model error, invalid modelFields: " + modelFields, e.getMessage());
            return RestResult.error(ExceptionCode.ConceptModel.CONCEPT_MODEL_SAVE_FAILED, "Insert concept model error, invalid modelFields.");
        }
        conceptModelService.saveConceptModel(conceptModel);
        return RestResult.sucess(conceptModel);
    }

    @ApiOperation("update concept model")
    @PostMapping("/update")
    public RestResult<ConceptModel> update(
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "projectId", required = true) String projectId,
            @RequestParam(value = "code", required = true) String code,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "type", defaultValue = "", required = false) String type,
            @RequestParam(value = "description", defaultValue = "", required = false) String description,
            @RequestParam(value = "modelFields", required = false) Map<String, String> modelFields) {
        ConceptModel conceptModel = new ConceptModel();
        conceptModel.setId(id);
        conceptModel.setCode(code);
        conceptModel.setName(name);
        conceptModel.setType(type);
        conceptModel.setProjectId(projectId);
        conceptModel.setDescription(description);
        conceptModel.setModelFields(modelFields);
        conceptModelService.updateConceptModel(conceptModel);
        return RestResult.sucess(conceptModel);
    }


}
